package kai.rating;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Rank extends Activity
{
	private static SQLiteDatabase db;
	private static ArrayList<HashMap<String,String>> name;
	private static HashMap<String,String> map;
	private static SimpleAdapter adapter;
	private static ListView listview;
	private static EloObject[] object;
	
	private static final String ORD_POINT="point desc";
	private static final String ORD_NAME="name asc";
	private static final String ORD_ID="rowid asc";
	
	private static String order=ORD_POINT;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rank);
		
		db=new DBHelper(this).getWritableDatabase();
		object=new EloObject[DBHelper.GetRows(db)];
		int row=DBHelper.GetRows(db);
		for(int i=0;i<row;i++)
			object[i]=new EloObject();
		DBHelper.GetObjs(db, object,order);

		name=new ArrayList<HashMap<String,String>>();
		for(int i=0;i<row;i++)
		{
			map=new HashMap<String,String>();
			map.put("name",i+1+"."+object[i].name);
			map.put("point",Integer.toString(object[i].point));
			name.add(map);
		}
		adapter=new SimpleAdapter(this,name,R.layout.list,new String[]{"name","point"},new int[]{android.R.id.text1,android.R.id.text2});
		listview=(ListView) findViewById(R.id.rank);
		listview.setAdapter(adapter);
		registerForContextMenu(listview);
		if(name.isEmpty())
			listview.setVisibility(8);//Gone
		else
			listview.setVisibility(0);//Visible
	}
	private static void Refresh()
	{
		DBHelper.GetObjs(db,object,order);
		name.clear();
		int row=DBHelper.GetRows(db);
		for(int i=0;i<row;i++)
		{
			map=new HashMap<String,String>();
			map.put("name",i+1+"."+object[i].name);
			map.put("point",Integer.toString(object[i].point));
			name.add(map);
		}
		adapter.notifyDataSetChanged();
		if(name.isEmpty())
			listview.setVisibility(8);//Gone
		else
			listview.setVisibility(0);//Visible
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo info)
	{
		super.onCreateContextMenu(menu, v, info);
		menu.add(0,0,0,R.string.delete);
		menu.add(0,1,1,R.string.deletepic);
	}
	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		AdapterContextMenuInfo info=(AdapterContextMenuInfo) item.getMenuInfo();
		switch(item.getItemId())
		{
		case 0:
			DBHelper.DeleteObjs(db,object[info.position].id);
			deleteFile(object[info.position].id+".png");
			break;
		case 1:
			deleteFile(object[info.position].id+".png");
			break;
		}
		Refresh();
		return super.onContextItemSelected(item);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(0,0,0,R.string.sort_name);
		menu.add(0,1,1,R.string.sort_point);
		menu.add(0,2,2,R.string.sort_added);
		return true;
	}
	@Override
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		if(order=="name asc")
		{
			menu.getItem(0).setVisible(false);
			menu.getItem(1).setVisible(true);
			menu.getItem(2).setVisible(true);
		}
		else if(order=="point desc")
		{
			menu.getItem(0).setVisible(true);
			menu.getItem(1).setVisible(false);
			menu.getItem(2).setVisible(true);
		}
		else if(order=="rowid asc")
		{
			menu.getItem(0).setVisible(true);
			menu.getItem(1).setVisible(true);
			menu.getItem(2).setVisible(false);
		}
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId())
		{
		case 0:
			order=ORD_NAME;
			break;
		case 1:
			order=ORD_POINT;
			break;
		case 2:
			order=ORD_ID;
			break;
		}
		Refresh();
		return true;
	}
	@Override
	public void onBackPressed()
	{
		setResult(RESULT_OK);
		finish();
	}
}